package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.RelationshipMapper;
import com.example.demo.pojo.Relationship;
import com.example.demo.service.Rservice;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class RServiceImpl extends ServiceImpl<RelationshipMapper, Relationship> implements Rservice {
    @Override
    public List<Relationship> listByPersonId(Long personId) {
        return lambdaQuery()
                .eq(Relationship::getPerson_id, personId)
                .list();
    }
    @Override
    public List<Relationship> listByRelationType(String relationType) {
        return lambdaQuery()
                .eq(Relationship::getRelated_type, relationType)
                .list();
    }

    @Override
    public Page<Relationship> pageRelations(Page<Relationship> page, Long personId, String relationType) {
        LambdaQueryWrapper<Relationship> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(personId)) {
            queryWrapper.eq(Relationship::getPerson_id, personId);
        }
        if (StringUtils.hasText(relationType)) {
            queryWrapper.eq(Relationship::getRelated_type, relationType);
        }
        return page(page, queryWrapper);
    }

    @Override
    public boolean batchCreateRelations(List<Relationship> relationships) {
        return saveBatch(relationships);
    }
    @Override
    public boolean existsRelation(Long personId, Long relatedPersonId, String relationType) {
        return lambdaQuery()
                .eq(Relationship::getPerson_id, personId)
                .eq(Relationship::getRelated_person_id, relatedPersonId)
                .eq(Relationship::getRelated_type, relationType)
                .exists();
    }
}